import {
  getUserInfo
} from "../../apis/user";
import {
  getCardList
} from "../../apis/card";
import {
  notice
} from '../../apis/group';
const app = getApp();

Page({
  data: {
    userInfo: {},
    current: 0,
    isHaveNotice: false
  },

  onLoad() {

  },

  onShow() {
    this.getMyCard();
    this.getNotice();
  },

  onShareAppMessage() {
    return {
      title: `${this.data.cardList[this.data.current].userName}的快名片`,
      path: `/pages/cardPreview/index?type=share&cardId=${this.data.cardList[this.data.current].cardId}`,
      imageUrl: this.data.cardList[this.data.current].shareImage
    }
  },

  getMyCard() {
    getCardList({
      cardType: 1
    }).then(res => {
      if (res.data.code === 200) {
        let {
          rows
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('^https://').test(v.avatar) && !new RegExp('^http://').test(v.avatar)) v.avatar = app.globalData.url + v.avatar;
        });
        this.setData({
          cardList: rows
        });
        this.data.cardList.forEach((v, i) => {
          wx.getImageInfo({
            src: v.avatar,
            success: res => {
              let avatar = {
                path: res.path,
                width: res.width,
                height: res.height
              };
              this.canvasShareImage(avatar, v.userName, v.company, v.post ? v.post : '暂无', v.label, i);
            }
          });
        });
      };
    });
  },

  // 生成分享小卡片
  canvasShareImage(avatar, userName, company, post, label, index) {
    let cvs = wx.createCanvasContext(`share${index}`);
    cvs.drawImage('../../images/share_bg.png', 0, 0, 420, 336);
    cvs.save();
    cvs.beginPath();
    cvs.arc(210, 70, 54, 0, Math.PI * 2, false);
    cvs.clip();
    if (avatar.width > avatar.height) {
      cvs.drawImage(avatar.path, (avatar.width - avatar.height) / 2, 0, avatar.height, avatar.height, 156, 16, 108, 108);
    } else if (avatar.width < avatar.height) {
      cvs.drawImage(avatar.path, 0, (avatar.height - avatar.width) / 2, avatar.width, avatar.width, 156, 16, 108, 108);
    } else {
      cvs.drawImage(avatar.path, 156, 16, 108, 108);
    };
    cvs.restore();
    cvs.setFillStyle('#fff');
    cvs.setFontSize(26);
    let nameWidth = cvs.measureText(userName).width;
    cvs.fillText(userName, 210 - nameWidth / 2, 164);

    cvs.setFillStyle('#f7f7f7');
    cvs.setFontSize(20);
    let postWidth = cvs.measureText(`${company} · ${post}`).width;
    cvs.fillText(`${company} · ${post}`, 210 - postWidth / 2, 200);

    if (label.length > 0) {
      cvs.setFillStyle('#fff');
      cvs.setFontSize(22);
      label = label.map(v => {
        return {
          labelName: '#' + v,
          labelWidth: cvs.measureText('#' + v).width
        }
      }).sort((a, b) => b.labelWidth - a.labelWidth);
      let lineWidth = cvs.measureText('　|　').width;
      switch (label.length) {
        case 1:
          cvs.fillText(label[0].labelName, 210 - label[0].labelWidth / 2, 260);
          break;
        case 2:
          cvs.fillText(label[0].labelName + '　|　' + label[1].labelName, 210 - (label[0].labelWidth + label[1].labelWidth + lineWidth) / 2, 260);
          break;
        case 3:
          cvs.fillText(label[0].labelName + '　|　' + label[1].labelName, 210 - (label[0].labelWidth + label[1].labelWidth + lineWidth) / 2, 260);
          cvs.fillText(label[2].labelName, 210 - (label[2].labelWidth) / 2, 300);
          break;
        case 4:
          cvs.fillText(label[0].labelName + '　|　' + label[1].labelName, 210 - (label[0].labelWidth + label[1].labelWidth + lineWidth) / 2, 260);
          cvs.fillText(label[2].labelName + '　|　' + label[3].labelName, 210 - (label[2].labelWidth + label[3].labelWidth + lineWidth) / 2, 300);
          break;
        case 5:
          cvs.fillText(label[0].labelName + '　|　' + label[1].labelName, 210 - (label[0].labelWidth + label[1].labelWidth + lineWidth) / 2, 260);
          cvs.fillText(label[2].labelName + '　|　' + label[3].labelName + '　|　' + label[4].labelName, 210 - (label[2].labelWidth + label[3].labelWidth + label[4].labelWidth + lineWidth * 2) / 2, 300);
          break;
        default:
          break;
      };
    };

    cvs.draw(false, () => {
      wx.canvasToTempFilePath({
        canvasId: `share${index}`,
        destWidth: '420',
        destHeight: '336',
        fileType: 'png',
        success: res => {
          this.setData({
            [`cardList[${index}].shareImage`]: res.tempFilePath
          });
        }
      });
    });
  },

  // 消息
  getNotice() {
    notice().then(res => {
      if (res.data.code === 200) {
        let {
          applyNum,
          joinReplyNum
        } = res.data.data;
        if (applyNum + joinReplyNum > 0) {
          wx.showTabBarRedDot({
            index: 2
          });
          this.setData({
            isHaveNotice: true
          });
        } else if (applyNum + joinReplyNum == 0) {
          wx.hideTabBarRedDot({
            index: 2
          });
          this.setData({
            isHaveNotice: false
          });
        };
      };
    });
  },

  cardSwiperChange(e) {
    let {
      current
    } = e.detail;
    this.setData({
      current: current
    });
  },

  // 建组
  createGroup() {
    if (this.data.cardList.length === 1) {
      wx.navigateTo({
        url: `/pages/createGroup/index?type=createGroup&cardId=${this.data.cardList[0].cardId}`
      });
    } else {
      wx.navigateTo({
        url: '/pages/myCard/index?type=createGroup'
      });
    };
  },
  // 名片码
  cardQRcode() {
    if (this.data.cardList.length === 0) {
      wx.showToast({
        title: '您还没有名片',
        icon: 'none'
      });
    } else {
      wx.navigateTo({
        url: `/pages/qrCode/index?type=card&cardId=${this.data.cardList[this.data.current].cardId}`
      });
    };
  },

  // 获取用户信息
  getUserInfo() {
    getUserInfo().then(res => {
      if (res.data.code === 200) {
        let {
          user
        } = res.data.data;
        this.setData({
          userInfo: user
        });
      };
    })
  },

  // 我的名片
  jumpActions(e) {
    let {
      url
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: url
    });
  },

  // 查看名片
  previewCard(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/cardPreview/index?type=preview&cardId=${id}`
    });
  },

  // 反馈
  feedback() {
    wx.navigateTo({
      url: '/pages/feedback/index'
    });
  }
})